{include file="common/head"/}
<style>
    .layui-table-tips-main{max-height: none}
</style>
<div class="layui-tab layui-tab-brief" id="table-box">
    <ul class="layui-tab-title">
        <li class="layui-this" data-type = '1'>订单列表</li>
        <li data-type = '2'>已支付</li>
        <li data-type = '3'>未支付</li>
        <li data-type = '4'>待付款</li>
        <li data-type = '5'>已退款</li>
    </ul>
</div>
<div class="admin-main layui-anim layui-anim-upbit">
    <div class="demoTable">
        <div class="layui-inline">
            <input class="layui-input" name="key" id="key" placeholder="{:lang('pleaseEnter')}关键字">
        </div>
        <button class="layui-btn" id="search" data-type="reload">{:lang('search')}</button>
        <a href="{:url('index')}" class="layui-btn">显示全部</a>
        <button type="button" class="layui-btn layui-btn-danger" id="delAll">批量删除</button>
        <div style="clear: both;"></div>
    </div>
    <table class="layui-table" id="list" lay-filter="list"></table>
</div>
{include file="common/foot"/}
<script type="text/html" id="name">
    {{# if(d.pic){ }}
        <img src="__PUBLIC__/{{d.pic}}">
    {{# }else{ }}
        <i class="fa fa-image"></i>
    {{# } }}

</script>
<script type="text/html" id="order">
    <input name="{{d.course_id}}" data-id="{{d.course_id}}" class="list_order layui-input" value=" {{d.sort}}" size="10"/>
</script>

<script type="text/html" id="open">
    {{# if(d.status==1){ }}
        <button class="layui-btn layui-btn-mini layui-btn-normal" >已审核</button>
    {{# }else if(d.status == 2){  }}
        <button class="layui-btn layui-btn-mini layui-btn-warm" >已审核</button>
    {{# }else{ }}
        <button class="layui-btn layui-btn-mini layui-btn-danger" >已禁</button>
    {{# } }}
</script>
<script type="text/html" id="action">
    <a href="javascript:;" class="layui-btn layui-btn-normal layui-btn-mini">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-mini" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-mini" lay-event="details">详情</a>
</script>
<script>
    layui.use('table', function() {
        var table = layui.table, $ = layui.jquery;
        var tableIn = table.render({
            id: 'ad',
            elem: '#list',
            url: '{:url("index")}',
            method: 'post',
            page:true,
            cols: [[
                {checkbox: true, fixed: true},
                {field: 'order_id', title: 'id', width: 80, fixed: true},
                {field: 'order_sn', title: '订单编号', width: 200},
                {field: 'pic', title: '课程图片', width: 100,templet: '#name'},
                {field: 'course_name', title: '课程名称', width: 400},
                {field: 'addtime', title: '{:lang("add")}{:lang("time")}',width: 150},
                //{field: 'sort', align: 'center', title: '{:lang("order")}', width: 120, templet: '#order'},
                {field:'status', title:'状态', width:85, templet: '#open', unresize: true},
                //{field: 'status', align: 'center', title: '{:lang("status")}', width: 100, toolbar: '#open'},
                {title:'操作',width: 160, align: 'center', toolbar: '#action'}
            ]],
            limit:10
        });

        //搜索
        $('#search').on('click', function () {
            var key = $('#key').val();
            if ($.trim(key) === '') {
                layer.msg('{:lang("pleaseEnter")}关键字！', {icon: 0});
                return;
            }
            tableIn.reload({
                where: {key: key}
            });
        });


        table.on('tool(list)', function(obj) {
            var data = obj.data;
            if (obj.event === 'open') {
                var loading = layer.load(1, {shade: [0.1, '#fff']});
                $.post('{:url("editState")}', {'id': data.ad_id}, function (res) {
                    layer.close(loading);
                    if (res.status === 1) {
                        if (res.open === 1) {
                            obj.update({
                                open: '<a class="layui-btn layui-btn-mini layui-btn-warm" lay-event="open">开启</a>'
                            });
                        } else {
                            obj.update({
                                open: '<a class="layui-btn layui-btn-mini layui-btn-danger" lay-event="open">禁用</a>'
                            });
                        }
                    } else {
                        layer.msg('操作失败！', {time: 1000, icon: 2});
                        return false;
                    }
                })
            }else if(obj.event === 'del'){
                layer.confirm('您确定要删除该广告吗？', function(index){
                    var loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("{:url('del')}",{order_id:data.order_id},function(res){
                        layer.close(loading);
                        if(res.code===1){
                            layer.msg(res.msg,{time:1000,icon:1});
                            tableIn.reload();
                        }else{
                            layer.msg('操作失败！',{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                });
            }else if(obj.event === 'details'){
                layer.alert('您是要查看详细吗');
            }
        });
        $('body').on('blur','.list_order',function() {
            var course_id = $(this).attr('data-id');
            var sort = $(this).val();
            var loading = layer.load(1, {shade: [0.1, '#fff']});
            $.post('{:url("adOrder")}',{course_id:course_id,sort:sort},function(res){
                layer.close(loading);
                if(res.code === 1){
                    layer.msg(res.msg, {time: 1000, icon: 1});
                    tableIn.reload();
                }else{
                    layer.msg(res.msg,{time:1000,icon:2});
                }
            })
        });
        $('#delAll').click(function(){
            layer.confirm('确认要删除选中的广告吗？', {icon: 3}, function(index) {
                layer.close(index);
                var checkStatus = table.checkStatus('ad'); //test即为参数id设定的值
                var ids = [];
                $(checkStatus.data).each(function (i, o) {
                    ids.push(o.course_id);
                });
                var loading = layer.load(1, {shade: [0.1, '#fff']});
                $.post("{:url('delall')}", {ids: ids}, function (data) {
                    layer.close(loading);
                    if (data.code === 1) {
                        layer.msg(data.msg, {time: 1000, icon: 1});
                        tableIn.reload();
                    } else {
                        layer.msg(data.msg, {time: 1000, icon: 2});
                    }
                });
            });
        })
    })
</script>